package com.huawei.petSystemdemo.data;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huawei.petSystemdemo.bean.Pets;
import com.huawei.petSystemdemo.bean.Shop;
import com.huawei.petSystemdemo.utils.MyRequest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

public class ShopRepository {
    
    public static void getAll(int current, int size, Consumer<List<Shop>> callback) {
        Map<String, Object> body = new HashMap<>();
        body.put("size", size);
        body.put("current", current);
        MyRequest.get("/shop/getAll", body, json -> {
            JSONObject obj = JSON.parseObject(json);
            boolean success = obj.getBoolean("success");
            if (success)
            {
                List<Shop> shopList = obj.getJSONArray("data").toJavaList(Shop.class);
                if (callback != null)
                    callback.accept(shopList);
            }
            else
            {
            }
        });
    }

    public static void getByShopId(int shopId, Consumer<Shop> callback) {
        Map<String, Object> body = new HashMap<>();
        body.put("id", shopId);
        MyRequest.get("/shop/getOne", body, json -> {
            JSONObject obj = JSON.parseObject(json);
            boolean success = obj.getBoolean("success");
            if (success)
            {
                Shop shop = obj.getObject("data", Shop.class);
                if (callback != null)
                    callback.accept(shop);
            }
            else
            {

            }
        });
    }
}
